﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Dataglot;

namespace IrishSettings
{
    public class SelectApplicationSettingValuesByVersion:GenericCommand
    {
        public SelectApplicationSettingValuesByVersion(DataFactory factory,DbConnection shared, DbTransaction transaction):base(factory,shared, transaction)
        {
            ConfigureCommand();
        }

        public SelectApplicationSettingValuesByVersion(DataFactory factory) : base(factory)
        {
            ConfigureCommand();
        }

        private void ConfigureCommand()
        {
            const string sql = @"
SELECT    {$ns}_settings_values.id , {$ns}_settings_applications.application , {$ns}_settings_applications.version , {$ns}_settings_schema.name , 
                      {$ns}_settings_schema.scope , {$ns}_settings_schema.default_serialized_value , {$ns}_settings_schema.value_type , 
                      {$ns}_settings_schema.is_user , {$ns}_settings_schema.is_anonymous , {$ns}_settings_applications.culture , 
                      {$ns}_settings_values.serialized_value , {$ns}_settings_values.value_type 
FROM         {$ns}_settings_applications INNER JOIN
                      {$ns}_settings_schema ON {$ns}_settings_applications.id = {$ns}_settings_schema.application_id INNER JOIN
                      {$ns}_settings_values ON {$ns}_settings_schema.id = {$ns}_settings_values.schema_id
where 
    {$ns}_settings_applications.application = @application
and {$ns}_settings_applications.version =  @version
    ";
            ComposeSql(sql);
            AddParameter("@application", DbType.String);
            AddParameter("@version", DbType.String);
        }

        public DbDataReader Select(
            string application, 
            Version version
            )
        {
            if (string.IsNullOrEmpty(application))
                throw new ArgumentException("Missing application name", "application");
            if (version==null)
                throw new ArgumentException("Missing version", "version");
            
            command.Parameters["@application"].Value = application;
            command.Parameters["@version"].Value = version.ToString();
            return ExecuteReader(CommandBehavior.SingleResult);
        }

        public List<long> Ids(
            string application,
            Version version
            )
        {
            if (string.IsNullOrEmpty(application))
                throw new ArgumentException("Missing application name", "application");
            if (version == null)
                throw new ArgumentException("Missing version", "version");

            command.Parameters["@application"].Value = application;
            command.Parameters["@version"].Value = version.ToString();
            
            List<long> list = new List<long>();
            using(DbDataReader reader = ExecuteReader(CommandBehavior.SingleResult))
            {
                if(reader.HasRows)
                {
                    while(reader.Read())
                    {
                        list.Add(Convert.ToInt64(reader[0]));
                    }
                }
            }

            return list;
        }

    }
}
